<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/7
 * Time: 15:27
 */

namespace app\plat\controller;


use app\common\attr;
use think\Db;

class Statistics extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $goods_type = attr::get_list('good_class');
        $goods = Db::name('goods')->select();
        $this->assign('goods_type',json_encode($goods_type));
        $this->assign('goods',json_encode($goods));
        return $this->fetch();
    }

    public function ajax_get_count_data()
    {
        $time1 = input('start');
        $time2 = input('end');
        $start = $end = null;
        $type = input('type_id/d');
        $class_id = input('class_id/d');
        $goods_id = input('goods_id/d');
        if($time1 && $time2){
            $start = strtotime($time1);
            $end = strtotime($time2)+24*3600-1;
        }

        $where['pay_time'] = ['between',[$start,$end]];
        if($type){
            $where['a.type'] = ['eq',$type];
        }
        if($class_id!=''){
            $where['b.class_id'] =  ['eq',$class_id];
        }
        if($goods_id!=''){
            $where['a.goods_id'] =  ['eq',$goods_id];
        }
        if(!$where){
            $where = '1=1';
        }

        $data = Db::name('order')
            ->alias('a')
            ->join('goods b','b.id=a.goods_id','left')
            ->where($where)
            ->where('a.status',2)
            ->field('sum(amount) as amount,sum(pay_amount) as pay_amount,sum(points) as points,
            sum(discount_amount) as discount_amount,sum(member_amount) as member_amount')
            ->find();
        echo api_return(1,'',$data);

    }


}